﻿#region Imports
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Data;
using CommonUtilities.Extensions;
using PlayRandom.Models;
#endregion Imports

namespace PlayRandom.Converters
{
    public class GroupedFavouritesConverter : IValueConverter
    {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ObservableCollection<FileDetails> allFavFiles = value as ObservableCollection<FileDetails>;
            var directories = allFavFiles.Coalesce(new ObservableCollection<FileDetails>())
                .GroupBy(fav => Path.GetDirectoryName(fav.FilePath)).Select(fav => new { DirectoryPath = fav.Key, FavCount = fav.Count() });

            return directories.OrderBy(fav => fav.DirectoryPath).ToDictionary(fav => String.Format("{0} \t» {1}", ("x" + fav.FavCount).PadLeft(6, '_'), fav.DirectoryPath), fav => allFavFiles.Where(z => Path.GetDirectoryName(z.FilePath) == fav.DirectoryPath));
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion IValueConverter Members
    }
}
